#include "wx/wxprec.h"
#include "wx/dcbuffer.h"

#ifndef WX_PRECOMP
#include "wx/module.h"
#endif

class wxSharedDCBufferManager : public wxModule {
  public:
    wxSharedDCBufferManager() { }

    virtual bool OnInit() { return true; }
    virtual void OnExit() { wxDELETE( ms_buffer ); }

    static wxBitmap* GetBuffer( int w, int h ) {
      if( !ms_buffer || w > ms_buffer->GetWidth() || h > ms_buffer->GetHeight() ) {
        delete ms_buffer;
        if( !w ) {
          w = 1;
        }
        if( !h ) {
          h = 1;
        }
        ms_buffer = new wxBitmap( w, h );
      }
      return ms_buffer;
    }

  private:
    static wxBitmap *ms_buffer;

    DECLARE_DYNAMIC_CLASS( wxSharedDCBufferManager )
};

wxBitmap* wxSharedDCBufferManager::ms_buffer = NULL;
IMPLEMENT_DYNAMIC_CLASS( wxSharedDCBufferManager, wxModule )

void wxBufferedDC::UseBuffer( wxCoord w, wxCoord h ) {
  if( !m_buffer || !m_buffer->IsOk() ) {
    if( w == -1 || h == -1 ) {
      m_dc->GetSize( &w, &h );
    }
    m_buffer = wxSharedDCBufferManager::GetBuffer( w, h );
  }
  SelectObject( *m_buffer );
}

